package action;

import entity.User;
import exception.DuplicateUsernameException;
import exception.UserDisabledException;
import exception.UserNotFoundException;
import framework.bean.factory.ObjectFactory;
import framework.mvc.annotation.Action;
import framework.mvc.annotation.Controller;
import framework.mvc.annotation.Result;
import framework.mvc.enu.ResultType;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import service.UserService;
import util.ActionFormUtil;
import vo.AjaxResult;

@Controller
@Action(path = "/user")
public class UserAction {
    @Action(path = "/showIndex",forward = "/WEB-INF/page/index.jsp")
    public void showIndex(){}

    @Action(path = "/showRegist",forward = "/WEB-INF/page/regist.jsp")
    public void showRegist(){}

    @Action(path = "/showLogin",forward = "/WEB-INF/page/login.jsp")
    public void showLogin(){}

    @Action(path = "/showSuccess",forward = "/WEB-INF/page/success.jsp")
    public void showSuccess(){}

    @Action(path = "/regist",results = {
            @Result(name = "success",path = "/user/showLogin.action",type = ResultType.REDIRECT),
            @Result(name = "duplicateUsername",path = "/WEB-INF/page/regist.jsp",type = ResultType.FORWARD)
    })
    public String regist(HttpServletRequest request, HttpServletResponse response) throws ServletException {

        User user= ActionFormUtil.parseRequest(request,User.class);

        UserService userService= (UserService) ObjectFactory.getObject("userService");

        try {
            userService.regist(user);

            return "success";
        } catch (DuplicateUsernameException e) {
            request.setAttribute("message",e.getMessage());

            return "duplicateUsername";
        }
    }

    @Action(path = "/login",results = {
            @Result(name = "success",path = "/user/showSuccess.action",type = ResultType.REDIRECT),
            @Result(name = "fail",path = "/WEB-INF/page/login.jsp",type = ResultType.FORWARD)
    })
    public String login(HttpServletRequest request,HttpServletResponse response)throws ServletException{
        String username=request.getParameter("username");
        String password=request.getParameter("password");

        UserService userService= (UserService) ObjectFactory.getObject("userService");

        try {
            User user=userService.login(username,password);

            request.getSession().setAttribute("user",user);

            return "success";
        } catch (UserNotFoundException e) {
            request.setAttribute("message",e.getMessage());

            return "fail";
        } catch (UserDisabledException e) {
            request.setAttribute("message",e.getMessage());

            return "fail";
        }
    }

    @Action(path = "/validateUsername",results = @Result(type = ResultType.JSON))
    public AjaxResult validateUsername(HttpServletRequest request, HttpServletResponse response) throws ServletException{
        String username=request.getParameter("username");

        UserService userService= (UserService) ObjectFactory.getObject("userService");

        try {
            userService.validateUsername(username);

            return AjaxResult.success("用户名可用");
        } catch (DuplicateUsernameException e) {
            return new AjaxResult(AjaxResult.AJAX_CODE_EXCEPTION,e.getMessage(),null);
        }
    }


}

















